package com.zhouhailin.shirospringbootdemo.controller;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.zhouhailin.shirospringbootdemo.utils.JWTUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zhouhailin
 * @date 2023/1/11 15:12
 */
@RestController
public class TokenTestController {
    private static final Logger logger = LoggerFactory.getLogger(TokenTestController.class);

    @PostMapping("/token/test")
    public Map<String, Object> test(String token) {
        Map<String, Object> map = new HashMap<>();
        try {
            JWTUtils.verify(token);
            map.put("msg", "验证通过~~~");
            map.put("state", true);
        } catch (TokenExpiredException e) {
            map.put("state", false);
            map.put("msg", "Token已经过期!!!");
            logger.error("Token已经过期!!!", e);
        } catch (SignatureVerificationException e){
            map.put("state", false);
            map.put("msg", "签名错误!!!");
            logger.error("签名错误!!!", e);
        } catch (AlgorithmMismatchException e){
            map.put("state", false);
            map.put("msg", "加密算法不匹配!!!");
            logger.error("加密算法不匹配!!!", e);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "无效token~~");
            logger.error("无效token~~", e);
        }
        return map;
    }


}
